#!/usr/bin/env ruby

$stdout.sync = true # persistent process

require 'json'
opt = ARGV[0] ? JSON::parse(ARGV[0]) : {'interval' => 1}

p = IO.popen([*%w{vmstat}, opt['interval'].to_s], 'r')
p.readline # scrap
optional = {stolen: 'st', wait: 'wa'}
want = {user: 'us', system: 'sy', idle: 'id'}.
  merge(optional)

# index the header
head = p.readline.chomp.sub(/^\s+/, '').split(/\s+/)
head = Hash[*head.zip(0..(head.length-1)).flatten]

outs = {}; want.each {|k,v|
  if head.include?(v)
    outs[k] = head[v]
  else
    raise "missing key '#{v}' (#{k}) - expected in header" \
      unless optional.include?(k)
  end
}

# discard first (unprimed) output
p.readline; puts '{}'

while l = p.readline.sub(/^\s+/, '')
  if l =~ /\d+\s+\d/
    l = l.split(/\s+/)
    puts JSON::generate(
      Hash[*outs.keys.map {|k| [k, l[outs[k]]]}.flatten]
    )
  else
    # TODO verify that this is a header line and not something strange
    # puts '{}'
  end
end
